package control;

import model.object.ObjectUser;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.NumberPicker.OnValueChangeListener;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.talknow.R;
import common.Const;

public class Screen2Activity extends Activity implements OnClickListener,
		OnValueChangeListener {

	NumberPicker picker1, picker2, picker3;

	String[] list_sex, list_age, list_distance;
	String[] list_sex_value, list_age_value, list_distance_value;
	int sexPosition = 0, agePosition = 0, distancePosition = 0;

	ObjectUser objUser;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_screen_2);

		picker1 = (NumberPicker) findViewById(R.id.screen_2_picker_sex);
		picker2 = (NumberPicker) findViewById(R.id.screen_2_picker_age);
		picker3 = (NumberPicker) findViewById(R.id.screen_2_picker_distance);

		((Button) findViewById(R.id.screen_2_button_your_spec))
				.setOnClickListener(this);

		list_sex = new String[3];
		list_sex[0] = Const.SEX_WOMAN;
		list_sex[1] = Const.SEX_MAN;
		list_sex[2] = Const.SEX_FREE;

		list_sex_value = new String[3];
		list_sex_value[0] = Const.SEX_WOMAN_VALUE;
		list_sex_value[1] = Const.SEX_MAN_VALUE;
		list_sex_value[2] = Const.SEX_FREE_VALUE;

		list_age = new String[5];
		list_age[0] = Const.AGE_18;
		list_age[1] = Const.AGE_19_28;
		list_age[2] = Const.AGE_29_38;
		list_age[3] = Const.AGE_39;
		list_age[4] = Const.AGE_FREE;

		list_age_value = new String[5];
		list_age_value[0] = Const.AGE_18_VALUE;
		list_age_value[1] = Const.AGE_19_28_VALUE;
		list_age_value[2] = Const.AGE_29_38_VALUE;
		list_age_value[3] = Const.AGE_39_VALUE;
		list_age_value[4] = Const.AGE_FREE_VALUE;

		list_distance = new String[4];
		list_distance[0] = Const.distance_5;
		list_distance[1] = Const.distance_100;
		list_distance[2] = Const.distance_500;
		list_distance[3] = Const.distance_FREE;

		list_distance_value = new String[4];
		list_distance_value[0] = Const.distance_5_VALUE;
		list_distance_value[1] = Const.distance_100_VALUE;
		list_distance_value[2] = Const.distance_500_VALUE;
		list_distance_value[3] = Const.distance_FREE_VALUE;

		picker1.setMinValue(0);
		picker1.setMaxValue(list_sex.length - 1);
		picker1.setDisplayedValues(list_sex);
		picker1.setOnValueChangedListener(this);

		picker2.setMinValue(0);
		picker2.setMaxValue(list_age.length - 1);
		picker2.setDisplayedValues(list_age);
		picker2.setOnValueChangedListener(this);

		picker3.setMinValue(0);
		picker3.setMaxValue(list_distance.length - 1);
		picker3.setDisplayedValues(list_distance);
		picker3.setOnValueChangedListener(this);

		AdView adView = (AdView) this.findViewById(R.id.app_admob);
		AdRequest request = new AdRequest.Builder()
				.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
				.addTestDevice("9234EBAE83E894E6271DEBE7B380782E").build();
		adView.loadAd(request);

		initData();
	}

	void initData() {
		try {
			objUser = (ObjectUser) getIntent().getExtras().getSerializable(
					Const.KEY_OBJECT);
			for (int i = 0; i < list_sex_value.length; i++) {
				if (list_sex_value[i].equals(objUser.getTargetsex())) {
					picker1.setValue(i);
					break;
				}
			}
			for (int i = 0; i < list_age_value.length; i++) {
				if (list_age_value[i].equals(objUser.getTargetage())) {
					picker2.setValue(i);
					break;
				}
			}
			for (int i = 0; i < list_distance_value.length; i++) {
				if (list_distance_value[i].equals(objUser.getTargetdistance())) {
					picker3.setValue(i);
					break;
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (objUser == null)
			objUser = new ObjectUser();
		objUser.setTargetsex(list_sex_value[sexPosition]);
		objUser.setTargetage(list_age_value[agePosition]);
		objUser.setTargetdistance(list_distance_value[distancePosition]);

		Intent intent = new Intent(Screen2Activity.this, Screen3Activity.class);
		Bundle bundle = new Bundle();
		bundle.putSerializable(Const.KEY_OBJECT, objUser);
		intent.putExtras(bundle);
		startActivityForResult(intent, 1);
	}

	@Override
	public void onValueChange(NumberPicker arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.screen_2_picker_sex:
			sexPosition = arg2;
			break;
		case R.id.screen_2_picker_age:
			agePosition = arg2;
			break;
		case R.id.screen_2_picker_distance:
			distancePosition = arg2;
			break;

		default:
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 1 && resultCode == RESULT_OK) {
			try {
				objUser = (ObjectUser) data.getExtras().getSerializable(
						Const.KEY_OBJECT);
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}

}
